
const cloud = getApp().cloud;
const app = getApp();

Page({
  data: {
    fixedObj:{
      w:218,    
      h:727,
      x:134,
      y:20
    },

    moveObj:[
      {
          w:196,    
          h:232,
          x:8,
          y:20,
          color:"#000",
          disabled:false,
          opacity:1,
          imgRef:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN013kjE9h20xZrUkeQ4C_!!4161056916.png",     //口味
          isImgRef:''     //对应主体
      },
      {
          w:196,    
          h:232,
          x:8,
          y:140,
          color:"pink",
          disabled:false,
          opacity:1,
          imgRef:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN013kjE9h20xZrUkeQ4C_!!4161056916.png",
          isImgRef:''
      },
      {
          w:196,    
          h:232,
          x:8,
          y:260,
          color:'#666',
          disabled:false,
          opacity:1,
          imgRef:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN013kjE9h20xZrUkeQ4C_!!4161056916.png",
          isImgRef:''
      },
      {
          w:196,    
          h:232,
          x:266,
          y:20,
          color:'green',
          disabled:false,
          opacity:1,
          imgRef:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN013kjE9h20xZrUkeQ4C_!!4161056916.png",
          isImgRef:''
      },
      {
          w:196,    
          h:232,
          x:266,
          y:140,
          color:"red",
          disabled:false,
          opacity:1,
          imgRef:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN013kjE9h20xZrUkeQ4C_!!4161056916.png",
          isImgRef:''
      },
      {
          w:196,    
          h:232,
          x:266,
          y:260,
          color:"yellow",
          disabled:false,
          opacity:1,
          imgRef:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN013kjE9h20xZrUkeQ4C_!!4161056916.png",
          isImgRef:''
      }
   ],
   defaultImg:'https://img.alicdn.com/imgextra/i2/4161056916/O1CN01VsGya320xZrUEbqCk_!!4161056916.png',
   isDisabled:false,    //拖动块禁用
   num:7,    //选了哪个拖动块
   isTouchStart:false
  },

  zt(){

    app.globalData.innerAudioContext.pause()

  },

  bf(){

    app.globalData.innerAudioContext.play()

  },

  movableChange(e){

    this.setData({
      isTouchStart:true
    })

    let kType = e.currentTarget.dataset.type    //移动的对象的下标
    let moveObjX = e.detail.x      //移动对象的x轴
    let moveObjY = e.detail.y      //移动对象的Y轴

    console.log(e);

    this.isConten(kType,moveObjX,moveObjY);

    //this.onTouchEnd(kType,moveObjX,moveObjY);

  },

  // 处理拖动的对象是否在区域内
  isConten(t,x,y){

    this.moveObj = this.data.moveObj

    console.log(this.data.moveObj[t],x,y);

    let color = this.data.moveObj[t].color

    if(x > this.data.fixedObj.x - (this.data.moveObj[t].w * this.data.bili) / 2 && x < (this.data.fixedObj.x + this.data.bili * this.data.fixedObj.w - (this.data.moveObj[t].w * this.data.bili) / 2)){

      console.log("到了",t);

      this.setData({

        [`moveObj[${t}].opacity`]: 0,

        [`moveObj[${t}].x`]: this.data.moveObj[t].x + 0.01,

        defaultImg: this.data.moveObj[t].isImgRef,

        isDisabled: true,

        num: t,

        id: this.data.moveObj[t].id

      })

      console.log("xxxxxxxx===========>", this.data.moveObj[t].isImgRef)


    }else{

      //回到原位
      console.log(this.data.moveObj[t].x)

    }



    // 在区域内

    // if( x > this.data.fixedObj.x && x + (this.data.moveObj[t].w*this.data.bili) < this.data.fixedObj.x + (this.data.bili*this.data.fixedObj.w)){

    //   if( y > this.data.fixedObj.y && y + (this.data.moveObj[t].h*this.data.bili) < this.data.fixedObj.y + (this.data.bili*this.data.fixedObj.h)){

    //      console.log("对上了");

    //      this.setData({
    //        [`moveObj[${t}].opacity`]:0,
    //      })

    //      for(let i in this.data.moveObj){

    //         this.setData({
    //           [`moveObj[${i}].disabled`]:true,
    //         })

    //        if( i == t){

    //         console.log("11111",this.data.moveObj[0].x)

    //          this.setData({
              
    //             [`moveObj[${i}].x`]:this.data.moveObj[i].x+0.01,
    //             color:color

    //           })

    //        }else{
             
    //        }
           
    //      }
    //   }

    // }

  },


  //重置
  cz(){

     for(let i in this.data.moveObj){
        this.setData({
          [`moveObj[${i}].disabled`]:false,
          [`moveObj[${i}].opacity`]:1
        })
      }

      this.setData({
        color:'',
        isDisabled:false,
        num:7,
        defaultImg:'https://img.alicdn.com/imgextra/i2/4161056916/O1CN01VsGya320xZrUEbqCk_!!4161056916.png'
      })

  },


  //选好了
  yes(){

    if(this.data.num == 7){
      my.showToast({
        content:"您还未选择口味"
      })
    }else{
      
      this.selectKW();

    }

  },

  //获取口味列表
  getKWList(){
    cloud.function.invoke('client',{},'getFlavorList').then(res => {
      console.log('game---口味列表',res);  
      //itemLogo == isImgRef
      //mainLogo == imgRef
      for(let i in res.data){
        
        //数据库中两个数据存反了
        
        this.setData({
          [`moveObj[${i}].imgRef`]: res.data[i].itemLogo,
          [`moveObj[${i}].isImgRef`]: res.data[i].mainLogo,
          [`moveObj[${i}].id`]: res.data[i]._id
        }) 
      } 
    }) 
  },

  //选择了什么口味
  selectKW(){

    console.log(this.data.id);
    console.log(this.data.defaultImg);

    // return

    my.navigateTo({ url: `../game2/game2?id=${this.data.id}&defaultImg=${this.data.defaultImg}` })


    return

    cloud.function.invoke('client',{_id:this.data.id},'pickFlavor').then(res => {

      console.log('game---选择口味',res);  

      if(res.code == 0){
        my.redirectTo({ url: '../game2/game2' })
      }else{
        my.showToast({
          content:res.msg
        })
      }

      //itemLogo == isImgRef
      //mainLogo == imgRef

      // for(let i in res.data){

      //   this.setData({
      //     [`moveObj[${i}].imgRef`]: res.data[i].mainLogo,
      //     [`moveObj[${i}].isImgRef`]: res.data[i].itemLogo,
      //   })
        
      // } 

      
    }) 

  },


  

  onLoad() {

    this.setData({

      bili: my.getSystemInfoSync().windowWidth / 750

    })

    //口味列表
    this.getKWList();

  },

  onShow(){


  }

});
